package com.tenement.controller.foreground.house;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.base.exception.ParameterException;
import com.tenement.mybatis.entity.Collect;
import com.tenement.mybatis.entity.House;
import com.tenement.mybatis.entity.Photo;
import com.tenement.mybatis.entity.Reserve;
import com.tenement.mybatis.entity.User;
import com.tenement.service.interfaces.ICollectService;
import com.tenement.service.interfaces.IHouseService;
import com.tenement.service.interfaces.IPhotoService;
import com.tenement.service.interfaces.IReserveService;
import com.tenement.utils.base.ProduceUtil;

/**
 * 房源信息详情,20180417
 * 
 * @author wk
 *
 */
@Controller
public class HouseDetailController {

	private static final Log logger = LogFactory.getLog(HouseDetailController.class);

	String houseId = null;// 房屋唯一标识，使用hPhotoid代替,用于获取房源对应的图片列表

	@Autowired
	private IHouseService iHouseService;
	@Autowired
	private IPhotoService iPhotoService;
	@Autowired
	private ICollectService iCollectService;
	@Autowired
	private IReserveService iReserveService;

	/**
	 * 房屋详情，进入到某个页面
	 * 
	 * @param hPhotoid
	 * @return
	 */
	@RequestMapping(value = "/houseDetailHtml.action")
	public String houseDetai(String hPhotoid, HttpServletRequest request) {
		houseId = hPhotoid;
		logger.info("【-进入房源详情页面houseId-】" + houseId);
		List<Photo> photoList = iPhotoService.getHousePhotoList(houseId);// 获取房屋图片信息
		request.setAttribute("photoList", photoList);
		return "view/housedetail/housedetail";
	}
	
	@RequestMapping(value = "/houseDetaiBack.action")
	public String houseDetaiBack(String hPhotoid, HttpServletRequest request) {
		houseId = hPhotoid;
		logger.info("【-后台系统进入房源详情页面houseId-】" + houseId);
		List<Photo> photoList = iPhotoService.getHousePhotoList(houseId);// 获取房屋图片信息
		request.setAttribute("photoList", photoList);
		return "view/backend_system/housedetail";
	}

	/**
	 * 进入房屋详情界面，加载房屋详细属性,预约按钮，收藏按钮,需查看登录用户是否已经预约或收藏的该房源
	 * 
	 * @return
	 */
	@RequestMapping(value = "/loadingHouseDetail.action")
	public @ResponseBody JSONObject loadingHouseDetail(HttpSession session) {
		JSONObject jsonObject = new JSONObject();
		House house = iHouseService.queryHouseDetail(houseId);// 获取图片详细信息
		jsonObject.put("house", house);
		User user = (User) session.getAttribute("USERMSGKEY");// 获取登录中session中的信息
		int h_id = house.gethId();
		if (user != null) {
			int u_id = user.getuId();
			logger.info("【房源详情-判断用户是否收藏和收藏-参数】" + h_id + " " + u_id);
			Collect c = new Collect();
			c.sethId(h_id);
			c.setuId(u_id);
			String collectFlag = iCollectService.judgeIsCollected(c);
			jsonObject.put("collectFlag", collectFlag);// 1,0
			Reserve reserve = new Reserve();
			reserve.sethId(h_id);
			reserve.setuId(u_id);
			String reserveFlag = iReserveService.judgeIsReserved(reserve);
			jsonObject.put("reserveFlag", reserveFlag);// 1,0
		}
		logger.info("【房屋详细信息数据加载】" + jsonObject);
		return jsonObject;
	}

	/**
	 * 用户收藏操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "/userCollectHouses.action")
	public @ResponseBody String userCollectHouses(@RequestBody String data) {
		if (data != null) {
			JSONObject json = JSONObject.parseObject(data);
			String h_id = json.getString("h_id");// 收藏的房源ID
			String u_id = json.getString("u_id");// 房源收藏者id
			String c_collectdate = ProduceUtil.productNowDate();// 生成当前时间;
			logger.info("【参数】"+h_id+" "+u_id);
			Collect c = new Collect();
			c.setuId(Integer.parseInt(u_id));
			c.sethId(Integer.parseInt(h_id));
			c.setcCollectdate(c_collectdate);
			boolean b = iCollectService.userCollectHouses(c);
			if (b)
				return state.Success;// 收藏成功返回，
			else
				return state.Failed;// 收藏失败返回0，未知错误收藏失败;
		} else {
			try {
				throw new Exception("【收藏功能，传参为空】");
			} catch (Exception e) {
				logger.error("【收藏功能，传参为空】" + data);
				e.printStackTrace();
			}
		}
		return state.Failed;// 请求失败，返回标记0
	}

	/**
	 * 预约看房操作 
	 * userName userPhoneNumber hId uId
	 * @return
	 */
	@RequestMapping(value = "/userReserveHouses.action")
	public @ResponseBody String userReserveHouses(@RequestBody String data) {
		if (data != null) {
			JSONObject json = JSONObject.parseObject(data);
			String userName = json.getString("userName");//预约人姓名
			String userPhoneNumber = json.getString("userPhoneNumber");//预约人手机号
			String h_id = json.getString("hId");// 收藏的房源ID
			String u_id = json.getString("uId");// 房源收藏者id
			String r_Reservetdate = ProduceUtil.productNowDate();// 生成当前时间,预约时间
			Reserve r = new Reserve();
			r.sethId(Integer.parseInt(h_id));
			r.setuId(Integer.parseInt(u_id));
			r.setrReservedate(r_Reservetdate);
			boolean b = iReserveService.userReserveHouses(r,userName,userPhoneNumber);
			if (b)
				return state.Success;
			else
				return state.Failed;
		} else {
			try {
				throw new ParameterException("【预约看房功能，传参为空】"+data);
			} catch (ParameterException e) {
				e.printStackTrace();
			}
		}
		return state.Failed;
	}
}
